<?php
/*
 * employe/upload-rib.phtml
 */
$titre = "Téléchargement du fichier RIB";
$this->headTitle();

$site= isset($this->site)&& $this->site? $this->site: null;
$idSite =  $site ? (int) $site->id : -1;
$fichier = $this->fichier;

$dirRib = isset($this->dir_rib) ? str_replace('public/', '', $this->dir_rib)  : null; 

?>

<div class="container-fluid">
    <div class="content-head">
        <h4><?php echo $titre ;?></h4>
    </div>
    <div class="content-body content-body-site-form">
            <?php if (is_null($fichier)): ?>                              
                <?php if ($this->error) : ?>       
                <div class="info"><?php echo $this->error; ?></div>
                <?php else: ?>            
                <?php endif; ?>
            <?php else: ?>
                <div class="rib-info">
                    <?php $basePath = str_replace('public', '', $this->basePath()); ?>
                    <?php if (in_array($this->extension, array('jpg', 'png', 'gif'))) :?> 
                        <img src="<?php echo $this->basePath() . '/'. $dirRib . $this->destination ?>" width="200" height="250">
                    <?php else :?> 
                        <span> <?php echo $this->fichier; ?></span>
                    <?php endif ;?> 
                    
                </div>
                <div class="">
                    <span class="rib-warning">Vous devez-valider pour prendre en compte le fichier</span>
                    <button id="btn-valid" class="btn btn-info" style="float: right;" >Valider</button>
                </div>
                
            <?php endif; ?>
        
        <form id="site-rib-form" class="site-form well" action="<?php echo $this->url('RHX/default', array('controller' => 'site', 'action' => 'uploadRib')) ?>" method="post" enctype="multipart/form-data" method='POST'>
            <input type="file" id="site-rib-file" name="rib" onchange="submit();" accept="image/jpeg,image/gif,image/png">
            <input type="hidden" name="site_id" value="<?php echo $idSite; ?>">
        </form>
    </div>
</div>

<script type="text/javascript">
            $('#btn-valid').on('click', function(){
                <?php if ($idSite!== -1 ): // editing ?>         
                // MAJ de la base de données
                ajax = $.ajax({
                    type: 'post',
                    url: '<?php echo $this->url('RHX/default', array('controller' => 'site', 'action' => 'editData')) ?>',
                    data: '&pk=<?php echo $site->id; ?>&name=rib&value=' + encodeURI('<?php echo $this->destination; ?>'),
                });
                // MAJ de l'interface
                ajax.done(function(data){
		    linkViewRib = window.opener.document.getElementById('link-viewRib');
		    $(linkViewRib).attr('href', '<?php echo $this->basePath() . '/'. $dirRib . $this->destination ?>').show();
                    window.close();
                });
                ajax.error(function(data){
                    console.log('MAJ error');
                });
             <?php else: // adding ?> 
                dataPhotoRib = window.opener.document.getElementById('data-site-rib');
                $dataPhotoRib = $(dataPhotoRib);
                $dataPhotoRib.val("<?php  echo $this->destination ; ?>");
                
                dataRibSiteExt = window.opener.document.getElementById('data-site-rib_extension');
                $dataRibSiteExt  = $(dataRibSiteExt);
                $dataRibSiteExt .val ("<?php  echo $this->extension ; ?>");
                
		labelRib = window.opener.document.getElementById('label-uploadRib');
		$(labelRib).text('<?php echo $fichier; ?>').show();
		
                window.close();
            <?php endif; ?>    
            });
</script>
